#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sha1.h"
#define DLUGOSCHASLA 2

char newPassword[DLUGOSCHASLA];

void randomizeLetters(int seed, int size){
        char c;
		char out[DLUGOSCHASLA];// = (char *)malloc(size);
        int i;
        unsigned int temp_char;

        srand(seed);
        for(i=0;i<size;i++){
                temp_char = rand()%52;
                memcpy(&c, &temp_char, 1);
                if(c<26) /* Uppercase Alpha */
                        c += 65;
                else /* Lowercase Alpha */
                        c += 71;
                out[i] = c;
        }
        out[size] = '\0';
		memcpy(newPassword, out, DLUGOSCHASLA);
//		free(out);
}

int calculate1(unsigned char *hash){
		const unsigned int ratio = 256*256*256;
		unsigned int *cal;
        unsigned int i, j;
		unsigned char *cal_char;
		unsigned char temp[4];
		int ret;

		cal = (unsigned int*) malloc(sizeof(int));
        cal_char = (unsigned char*) cal;

        for(i=0;i<16;i++){
                for(j=0;j<4;j++)
                        temp[j] = hash[i+3-j];
                memcpy(cal, temp, sizeof(int));
                cal[0] = cal[0]%ratio;
                for(j=0;j<4;j++)
                        temp[j] = cal_char[3-j];
                memcpy(hash+i, temp, sizeof(int));
        }

        for(j=0;j<4;j++)
                cal_char[j] = hash[i+3-j];

		ret = cal[0];
		free(cal);
		return ret;
}

int calculate2(unsigned char*hash){
		const unsigned int ratio = 256*256*256;
		unsigned int *cal;
        unsigned int i, j;
		unsigned char *cal_char;
		unsigned char temp[4];
		int ret;

		cal = (unsigned int*) malloc(sizeof(int));
        cal_char = (unsigned char*) cal;

        for(i=0;i<16;i++){
                for(j=0;j<4;j++)
                        temp[j] = hash[i+3];
                memcpy(cal, temp, sizeof(int));
                cal[0] = cal[0]%ratio;
                for(j=0;j<4;j++)
                        temp[j] = cal_char[3-j];
                memcpy(hash+i, temp, sizeof(int));
        }

        for(j=0;j<4;j++)
                cal_char[j] = hash[i+3-j];
		ret = cal[0];
		free(cal);
		return ret;
}

void reduce(unsigned char *hash, unsigned int size){
		int seed;

		seed = calculate1(hash);
		randomizeLetters(seed, size);
}

void reduce2(unsigned char *hash, unsigned int size){
		int seed;

		seed = calculate2(hash);
		randomizeLetters(seed, size);
}

void reduce3(unsigned char *hash, unsigned int size){
		int seed;

		seed = calculate1(hash);
		randomizeLetters(~seed, size);
}

void reduce4(unsigned char *hash, unsigned int size){
		int seed;

		seed = calculate2(hash);
		randomizeLetters(~seed, size);
}

char* doTable(char *cos){
        int i;
        SHA1Context sha;
        uint8_t Message_Digest[20];// = (uint8_t*) malloc(20* sizeof(uint8_t));//[20];

		SHA1Reset(&sha);
        SHA1Input(&sha, (const unsigned char *) cos, DLUGOSCHASLA);
        SHA1Result(&sha, Message_Digest);
        
        for (i=0;i<25;i++){
				reduce(Message_Digest, DLUGOSCHASLA);
                SHA1Reset(&sha);
                SHA1Input(&sha, (const unsigned char *) newPassword, DLUGOSCHASLA);
                SHA1Result(&sha, Message_Digest);

                reduce2(Message_Digest, DLUGOSCHASLA);
                SHA1Reset(&sha);
                SHA1Input(&sha, (const unsigned char *) newPassword, DLUGOSCHASLA);
                SHA1Result(&sha, Message_Digest);

				reduce3(Message_Digest, DLUGOSCHASLA);
                SHA1Reset(&sha);
                SHA1Input(&sha, (const unsigned char *) newPassword, DLUGOSCHASLA);
                SHA1Result(&sha, Message_Digest);

                reduce4(Message_Digest, DLUGOSCHASLA);
                SHA1Reset(&sha);
                SHA1Input(&sha, (const unsigned char *) newPassword, DLUGOSCHASLA);
                SHA1Result(&sha, Message_Digest);
        }
        return (char *)Message_Digest;
}
